<?php
namespace app\agent\controller;
use think\Controller;
use think\Db;
use app\common\controller\AgentBaseController;
use app\agent\model\GoodsModel;


class Shop extends AgentBaseController
{
	
	protected function _initialize() {
        parent::_initialize();
    }
	//批量导入商品
	public function export_goods(){
		return $this->fetch();
	}
	public function padd_ok(){
		$fileName = 'public/upload/'.$_FILES['myfile']['name'];
		$arr_sql=dr($fileName);
		$f=array();
		$where['agent_id']=session('agent_id');
		$r=$arr_sql[0];
		$r=explode(',', $r);
		if($r[0]=="id"){
			$list;
			for($i=1;$i<count($arr_sql);$i++){
				$row=$arr_sql[$i];
				$row=explode(',', $row);
				$data['id']=$row['0'];
				$data['goods_no']=$row['1'];
				$data['goods_name']=$row['2'];
				$where['type_name']=trim($row['3']);
				$type=db('agent_type')->field("id")->where($where)->find();
				$data['trade_id']=$type['id'];
				$data['cost_price']=$row['4'];
				$data['sale_price']=$row['5'];
				$data['zs_price']=$row['6'];
				$data['dk_price']=$row['5']-$row['7'];
				$data['stock']=$row['8'];
				$data['is_recommend']=$row['9'];
				$data['chainstore_id']=$row['11'];
				$list[]=$data;
			}
			$p=implode(',', $f);
			if($list){
				Db::startTrans();
				$gd_model=new GoodsModel();
				$res=$gd_model->saveAll($list);
				if($res){
					Db::commit();
			 		$this->success('批量导入成功', url('goods_manage',['p'=>1]));
			 	}else{
			 		Db::rollback();
	        		$this->success('批量导入失败' , url('goods_manage',['p'=>1]));
				}
			}else{
				$this->success('批量导入失败' , url('goods_manage',['p'=>1]));
			}
		}else{
			for($i=1;$i<count($arr_sql);$i++){
				$row=$arr_sql[$i];
				$row=explode(',', $row);
				$data[$i-1]['goods_no']=$row['0'];
				$data[$i-1]['goods_name']=$row['1'];
				$where['type_name']=trim($row['2']);
				$type=db('agent_type')->field("id")->where($where)->find();
				$data[$i-1]['trade_id']=$type['id'];
				$data[$i-1]['cost_price']=$row['3'];
				$data[$i-1]['sale_price']=$row['4'];
				$data[$i-1]['zs_price']=$row['5'];
				$data[$i-1]['dk_price']=$row['4']-$row['6'];
				$data[$i-1]['stock']=$row['7'];
				$data[$i-1]['is_recommend']=$row['8'];
				$data[$i-1]['content']=$row['9'];
				$data[$i-1]['chainstore_id']=$row['10'];
				$data[$i-1]['agent_id']=session('agent_id');
				$data[$i-1]['oem_id']=session('oem_id');
				$data[$i-1]['addtime']= date("Y-m-d H:i:s");
			}
			$p=implode(',', $f);
			$data=isset($data)?$data:"";
			if($data){
				Db::startTrans();
				if(Db::name('agent_goods')->insertAll($data)){
					Db::commit();
			 		$this->success('批量导入成功', url('goods_manage',['p'=>1]));
			 	}else{
			 		Db::rollback();
	        		$this->success('批量导入失败' , url('goods_manage',['p'=>1]));
				}
			}else{
				$this->success('批量导入失败' , url('goods_manage',['p'=>1]));
			}
		}
	}
	//处理商品批量上下架 推荐
	public function chuli($status){
		if($status==3){
			$up['is_recommend']=1;
		}elseif($status==4){
			$up['is_recommend']=2;
		}elseif($status==5){
			$up['goods_state']=3;
		}else{
			$up['goods_state']=$status;
		}
		$where['id']=["in",input()['arr']];
		$res=db("agent_goods")->where($where)->update($up);
		if($res){
			return json(['code'=>200,'msg'=>'操作成功']);
		}else{
			return json(['code'=>400,'msg'=>'操作失败']);
		}
	}
	public function goods_code(){
		$goods = db('agent_goods')->find(input("goods_id"));
    	$agent_store = db('info_agent')->find($goods['agent_id']);
    	$code_url="http://".$_SERVER['HTTP_HOST']."/agent/shop/fast_buy?good_id=".$goods['id'];
    	
    	$agent_info['code_url'] = $code_url;
    	$agent_info['goods_name'] = $goods['goods_name'];
    	$agent_info['agent_name'] = $agent_store['agent_name'];
    	$agent_info['logo'] = $goods['thumb'];
    	$this->assign('agent_info',$agent_info);
		$chains=db("agent_store")->field("id,store_name")->where('agent_id',$goods['agent_id'])->select();
		$this->assign("chains",$chains);
    	return $this->fetch();
	}
	
	//批量修改门店
	function select_store(){
		
		$id_list = input('id_list');
		if($_POST){
			//获取门店id
			$chain_id = input('chain_id');
			$id_list = explode(",",$id_list);
			$where['id'] = ['in',$id_list];
			$resutl = db('agent_goods')->where($where)->update(['chainstore_id' => $chain_id]);
			if($resutl){
				return json(["code"=>200,'msg'=>'更新成功']);
			}else{
				return json(["code"=>403,'msg'=>'修改失败']);
			}
		}
		
		$chains=db("agent_store")
				->field("id,store_name")
				->where('agent_id',session('agent_id'))
				->where('state',1)
				->select();
		$this->assign("id_list",substr($id_list,0,strlen($id_list)-1));
		$this->assign("chains",$chains);
		return $this->fetch();
	}
	
	//批量修改分类
	function select_sort(){
		$id_list = input('id_list');
		if($_POST){
			//获取门店id
			$sort_id = input('sort_id');
			$id_list = explode(",",$id_list);
			$where['id'] = ['in',$id_list];
			$resutl = db('agent_goods')->where($where)->update(['trade_id' => $sort_id]);
			if($resutl){
				return json(["code"=>200,'msg'=>'更新成功']);
			}else{
				return json(["code"=>403,'msg'=>'修改失败']);
			}
		}
		
		$chains=db("agent_type")->field("id,type_name")->where('agent_id',session('agent_id'))->select();
		$this->assign("id_list",substr($id_list,0,strlen($id_list)-1));
		$this->assign("chains",$chains);
		return $this->fetch();
	}
	
	
	public function down_qr_code(){
		//获取当前商家信息
		$goods_id = input('id');
		$goods_id = substr($goods_id,0,strlen($goods_id)-1);
		$arr_id = explode(",", $goods_id); 
		
		$goods_arr = array();
		if(count($arr_id)>0){
			for($i=0;$i<count($arr_id);$i++){
				//获取商品信息
				$goods_info = db('agent_goods')->where('id',$arr_id[$i])->find();
				$code_url="http://".$_SERVER['HTTP_HOST']."/agent/shop/fast_buy?good_id=".$arr_id[$i];
				$goods_info['code_url'] = $code_url;
				array_push($goods_arr,$goods_info);
			}
		}else{
			return json(["code"=>403,'msg'=>'请选择商品']);
		}
		//获取商家信息
		$agent_info = db('info_agent')->find(session('agent_id'));
		//商家所有门店
		$chains=db("agent_store")->field("id,store_name")->where('agent_id',$agent_info['id'])->select();
		
		$this->assign('agent_info',$agent_info);
		$this->assign("chains",$chains);
		$this->assign("goods_arr",$goods_arr);
		
    	return $this->fetch();
	}
	
	//单产品快速付款页面
	public function fast_buy(){
		$goods=db('agent_goods')->find(input('good_id'));
		$chain_id=input("chain_id")?input("chain_id"):0;
		$this->assign("goods",$goods);
		$this->assign("chain_id",$chain_id);
		return $this->fetch();
	}
	public function ad_del(){
		$result = Db::table('agent_advertisement')->where('id',input("id"))->delete();
		if($result){
			return json(["code"=>200,'msg'=>'操作成功']);
		}else{
			return json(["code"=>400,'msg'=>'操作失败']);
		}
	}
	public function ad_edit(){
		if($_POST){
			$update['url']=input("url");
			$update['add_time']=now_time();
			$update['title']=input("title");
			$update['thumb']=input("logo");
			$update['agent_id']=session('agent_id');
			$res=db("agent_advertisement")->where("id",input("id"))->update($update);
		}
		$ad=db("agent_advertisement")->find(input("id"));
		$this->assign('ad',$ad);
		return $this->fetch();
	}
	public function ad_add(){
		if($_POST){
			$update['url']=input("url");
			$update['add_time']=now_time();
			$update['title']=input("title");
			$update['thumb']=input("logo");
			$update['agent_id']=session('agent_id');
			if(input("id")){
				$res=db("agent_advertisement")->where("id",input("id"))->update($update);
			}else{
				$res=db("agent_advertisement")->insert($update);
			}
			if($res){
				return json(['code'=>200,'msg'=>'操作成功']);
			}else{
				return json(['code'=>402,'msg'=>'操作失败']);
			}
		}
		return $this->fetch();
	}
		//鼓励金判断
	private function do_pay($nums,$goods,$agent,&$p_score,&$zs_score,&$pay_price,$user_id){
		if($pay_price<1){
			return;
		}
		if($agent['is_score']==1){//开启鼓励金营销
			$p_score=number_format($goods['dk_price'],4)*$nums;//抵扣积分
			$user=db('info_user')->field("score")->find($user_id);
		    if($p_score&&$user['score']>=$p_score){//用户积分抵扣
				$pay_price=$pay_price-$p_score;//抵扣后订单价格
			}else{//赠送鼓励金
				$p_score=0;
				$zs_score=number_format($goods['zs_price'],4)*$nums;//赠送积分
				if($agent['score']<$zs_score){
					header("Content-type: text/html; charset=utf-8"); 
					echo '商家鼓励金不足！！！';exit;
				}
			}			
		}
	}
		//添加日志
		function add_log($goods_num,$goods_id,$user_id,$agent_id,$chain_id,$emp_id,$sn,$pay_type,$state,$total_price,
		$pay_price,$oem_id){
			$data['goods_num']=$goods_num;
			$data['goods_id']=$goods_id;
			$data['uid']=$user_id;
			$data['agent_id']=$agent_id;
			$data['chain_id']=$chain_id;
			$data['clerk_id']=$emp_id;
			$data['order_type']=7;
			$data['message']='单产品下单购买';
			$data['order_sn']=$sn;
			$data['pay_type']=$pay_type;
			$data['state']=$state;
			$data['origin_price']=$total_price;
			$data['balance']=$pay_price;
			$data['oem_id']=$oem_id;
			$data['add_time']=now_time();
			$data['sy_type']=3;
			$res=db('log_paylog')->insert($data);
			return $res;
		}

	public function topay(){
		$goods_id=input("good_id");
		$chain_id=input("chain_id");
		$goods=db("agent_goods")->field("agent_id,dk_price,zs_price,oem_id,sale_price,stock")->find($goods_id);
		$agent=db('info_agent')->find($goods['agent_id']);
		$nums=input('nums');
		//检查商品库存
		if($goods['stock'] < $nums){
			dump('该商品已售罄'); exit;
		}
		$type=get_brower_type();//判断是微信还是支付宝打开
		$redirect=config('API_DOMAIN')."agent/shop/topay?good_id=$goods_id&chain_id=$chain_id&nums=$nums";
		if($type==1){
			$auth_code=input("auth_code");
			$url ='https://openauth.alipay.com/oauth2/publicAppAuthorize.htm?app_id='.config('alipay_appid').'&scope=auth_base&redirect_uri='.urlencode($redirect);
		}else{
			$auth_code=input("code");
			$url ='https://open.weixin.qq.com/connect/oauth2/authorize?appid='.config('wxpay_appid').'&redirect_uri='.urlencode($redirect).'&response_type=code&scope=snsapi_base&state=STATE%23wechat_redirect&connect_redirect=1#wechat_redirect';
		}
		if(!$auth_code){
			Header("Location: $url");
			exit;
		}else{
			if($type==1){//支付宝openid
				$field="alipay_id";
				$pay_type=3;
				$openid=user_ali_id($auth_code);
				
			}elseif($type==2){//微信openid
				$field="openid";
				$pay_type=2;
				if(session('cut_auth_code') != $auth_code){
					session('cut_auth_code',$auth_code);
					$openid=user_wx_id($auth_code);
				}else{
					Header("Location: $url");
					exit;
				}
			}	
			if($openid){
				$w_u[$field]=$openid;
				$user=db('info_user')->where($w_u)->find();
				if(!$user){
					$data[$field]=$openid;
					$data['level']			= $agent['user_id'];
					$data['oem_id']			= $agent['oem_id'];
					$data['agency_id1']		= $agent['agency_id1'];
					$data['agency_id2']		= $agent['agency_id2'];
					$data['salesman_id']	= $agent['salesman_id'];
					$data['tj_agent']		= $agent['id'];
					$data['add_time']		= now_time();
					
					$user_id=Db::name('info_user')->insertGetId($data);
				}else{
					$user_id=$user['id'];
				}			
			}
		}
		$p_score=0;//抵扣鼓励金
		$zs_score=0;//赠送鼓励金
		$price=$goods['sale_price']*$nums;//订单价
		$pay_price=$price;//实际支付价格
		$this->do_pay($nums,$goods,$agent,$p_score,$zs_score,$pay_price,$user_id);
		$sn=order_create_no();
		$log_res=$this->add_log($nums,$goods_id,$user_id,$agent['id'],$chain_id,'',$sn,$pay_type,1,$price,
		$pay_price,$agent['oem_id']);
		if($pay_price==0){//纯积分
			header("Content-type: text/html; charset=utf-8"); 
			echo '支付成功';exit;
		}
		if($type==1){//支付宝
	        if($agent['is_state']==1){
        		$url=pay_code($agent['cb_no'],$agent['cb_miyao'],$agent['cb_clerk'],$pay_price,1,$sn,'');
				Header("Location: $url");
				exit;
        	}else{
        		header("Content-type: text/html; charset=utf-8"); 
        		echo '支付失败';exit;
        	}
		}elseif ( $type==2 ) {//微信扫码
				 if($agent['is_state']==1){
        			$url=pay_code($agent['cb_no'],$agent['cb_miyao'],$agent['cb_clerk'],$pay_price,2,$sn,'');
					Header("Location: $url");
					exit;
	        	
	        	}else{
					header("Content-type: text/html; charset=utf-8"); 
	        		echo '支付失败';exit;
	        	}
		}    
	
	}
	//商品管理
	public function goods_manage()
    {
    	if(request()->isPost()){
    		
    		$param=input('post.');
			
			if(!empty($param['goods_no']))$where['a.goods_no'] = ['like','%'.$param['goods_no'].'%'];
			
			if(!empty($param['goods_name']))$where['a.goods_name'] = ['like','%'.$param['goods_name'].'%'];
			
			if(!empty($param['begin_time']) && !empty($param['end_time'])){
				$where['a.addtime'] = ['between time',[$param['begin_time'],$param['end_time']]];
			}
    	}
		$type_id=0;
		if(input('type_id')){
			$where['a.trade_id'] = input('type_id');	
			$type_id=input('type_id');
		}
		$this->assign('type_id',$type_id);
		
		$where['a.agent_id'] = session('agent_id');
		$where['a.goods_state'] = ['<>',3];
    	
		//获取当前店铺商品
		$goods_list = db('agent_goods')
						->alias('a')
						->field('a.id as goods_id,a.cost_price,a.goods_no,a.goods_name,a.sale_price,a.zs_price,a.goods_state,a.is_recommend,a.dk_price,a.addtime,b.id,b.type_name,a.stock,c.store_name')
						->join('agent_type b','a.trade_id = b.id','left')
						->join('agent_store c','a.chainstore_id = c.id and a.agent_id = c.agent_id','left')
						->where($where)
						->order('a.addtime desc')
						->paginate(20);
		
		$this->assign('goods_list',$goods_list);
		$costs = db('agent_goods')->alias('a')->where($where)->sum("cost_price");
		$this->assign('costs',$costs);
		//获取商品分类类型
		$goods_type = db('agent_type')->where('agent_id',session('agent_id'))->select();
		$this->assign('goods_type',$goods_type);
		$this->assign('empty','<span class="empty" >没有数据</span>');
		
    	return $this->fetch();
    }

	//商品管理导出
    public function exportout_dc(){
		$param=input();
		if(!empty($param['begin_time']) && !empty($param['end_time'])){
			$where['a.add_time'] = ['between time',[$param['begin_time'],$param['end_time']]];
		}
		if(!empty($param['goods_no']))$where['a.goods_no'] = $param['goods_no'];
		if(!empty($param['goods_name']))$where['a.goods_name'] = $param['goods_name'];
		if(!empty($param['type_id']))$where['a.trade_id'] = $param['type_id'];
			
		$where['a.agent_id'] = session('agent_id');
		$where['a.goods_state'] = ['<>',3];
    	
		//获取当前店铺商品
		$list = db('agent_goods')
				->alias('a')
				->field('a.content,a.is_recommend,a.cost_price,a.chainstore_id,a.id as goods_id,a.goods_no,a.goods_name,a.sale_price,a.zs_price,a.goods_state,a.is_recommend,a.dk_price,a.addtime,b.id,b.type_name,a.stock,c.store_name')
				->join('agent_type b','a.trade_id = b.id','left')
				->join('agent_store c','a.chainstore_id = c.id and a.agent_id = c.agent_id','left')
				->where($where)
				->order('a.addtime desc')
				->select();	
				
		$str ="id,商品编号,商品名称,商品分类,成本价,市场价,赠送鼓励金,会员价,库存,是否设为推荐1否2是,商品描述,门店编号"."\n";
        $str = iconv('utf-8','gbk',$str);
        foreach($list as $k=>$row){
        	$no= sprintf("%.0f", $row['goods_no']);
        	$goods_id= iconv('utf-8','gbk',$row['goods_id']);
        	$goods_no= iconv('utf-8','gbk',$no."\t");
        	$goods_name = iconv('utf-8','gbk',$row['goods_name'].'	');  
			$type_name = iconv('utf-8','gbk',$row['type_name'].'	');
			$cost_price=iconv('utf-8','gbk',$row['cost_price']);
			$price = iconv('utf-8','gbk',$row['sale_price']);
			$zs_price = iconv('utf-8','gbk',$row['zs_price']);
			$hyprice = iconv('utf-8','gbk',$row['sale_price']-$row['dk_price']);
			$stock = iconv('utf-8','gbk',$row['stock']);
			$istj = iconv('utf-8','gbk',$row['is_recommend']);
			$content=iconv('utf-8','gbk',$row['content']);
			$store_name = iconv('utf-8','gbk',$row['chainstore_id']);
			$str .= $goods_id.",".$goods_no.",".$goods_name.",".$type_name.",".$cost_price.",".$price.",".$zs_price.",".$hyprice.",".$stock.",".$istj.",".$content.",".$store_name.","."\n"; //用引文逗号分开 
        } 

	  	$filename ='商品列表.csv';   //设置文件名  
    	
        header("Content-type:text/csv"); 
        header("Content-Disposition:attachment;filename=".$filename); 
        header('Cache-Control:must-revalidate,post-check=0,pre-check=0'); 
        header('Expires:0'); 
        header('Pragma:public'); 
        echo $str; 
        exit;
	}
	
	//添加商品
    public function goods_add(){
    	//获取当前商家信息
    	$agent_id = session('agent_id');
		
    	if(request()->isPost()){
    		$param=input('post.');
			$param['addtime'] 	= now_time();
			$param['agent_id'] 	= session('agent_id');
			$param['oem_id']  	= session('oem_id');
			$agent_goods = new GoodsModel();
			$res = $agent_goods->allowField(true)->save($param);
			if($res){
				return json(['code'=>200,'msg'=>'添加成功','data'=>$param]);
			}else{
				return json(['code'=>402,'msg'=>'添加失败']);
			}
			
    	}
		//获取平台分类
		$platform_type = Db::table('sys_platform_type')->select();
		//1级分类
		$platform_1 = array();
		//2级分类
		$platform_2 = array();
		foreach($platform_type as $key=>$values){
			if($values['pid'] == 0){
				$platform_1[] = $values;
			}else{
				$platform_2[$values['pid']][] = $values;
			}
		}
		$this->assign('platform_1',$platform_1);
		$this->assign('platform_2',$platform_2);
		//获取商家分类
		$agent_type = Db::table('agent_type')->where('agent_id',$agent_id)->select();
		$this->assign('agent_type',$agent_type);
		$storelist=db('agent_store')->where('agent_id',session('agent_id'))->where('state',1)->select();
		$this->assign('storelist',$storelist);
		
    	return $this->fetch();
    }

	//修改商品
    public function goods_edit(){
    	
		if(request()->isPost()){
			$param=input('post.');
			$agent_goods = new GoodsModel();
			$res = $agent_goods->allowField(true)->save($param,$param['id']);
			
			if($res){
				return json(['code'=>200,'msg'=>'修改成功','data'=>$param]);
			}else{
				return json(['code'=>402,'msg'=>'修改失败']);
			}
			
		}
    	$goods_id = input('goods_id');
		$goods_info = db('agent_goods')
						->alias('a')
						->join('agent_type b','a.trade_id = b.id','left')
						->where('a.id',$goods_id)
						->find();
		if($goods_info){
			//商品信息
			$goods_info['content'] = html_entity_decode($goods_info['content']);
			$goods_info['goods_id'] = $goods_id;
			$this->assign('goods_info',$goods_info);
			
			//获取平台分类
			$platform_type = Db::table('sys_platform_type')->select();
			//1级分类
			$platform_1 = array();
			//2级分类
			$platform_2 = array();
			foreach($platform_type as $key=>$values){
				if($values['pid'] == 0){
					$platform_1[] = $values;
				}else{
					$platform_2[$values['pid']][] = $values;
				}
			}
			$this->assign('platform_1',$platform_1);
			$this->assign('platform_2',$platform_2);
			//获取商家分类
			$agent_type = Db::table('agent_type')->where('agent_id',session('agent_id'))->select();
			$this->assign('agent_type',$agent_type);
			$storelist=db('agent_store')->where('agent_id',session('agent_id'))->where('state',1)->select();
			$this->assign('storelist',$storelist);
			
		}else{
			$this->error('商品不存在');
		}
		
		return $this->fetch();
    }

	//删除商品
	function del_goods(){
		
		$id = input('goods_id');
		
		$res = db('agent_goods')->where('id',$id)->update(['goods_state'=>3]);
		if($res){
			$this->success('删除成功', url('agent/shop/goods_manage'));
		}else{
			$this->error('删除失败');
		}
		
	}




    //商品分类
	public function goods_type()
    {
    	$agentid = session('agent_id');
    	$list = Db::table('agent_type')->where('agent_id',$agentid)->order('id desc')->select();
        $this->assign('list',$list);
    	return $this->fetch();
    }
	
	
    public function goods_type_add()
    {
    	$agentid = session('agent_id');
    	$oemid = session('oem_id');
    	if($_POST){
    		$data['logo']=$_POST['logo'];
            $data['type_name']=$_POST['type_name'];
            $data['agent_id']=$agentid;
            $data['oem_id']=$oemid;
            $result=Db::table('agent_type')->insert($data);
            if($result){
			return json(['code'=>200,'msg'=>'成功']);
	        }else{
	       		return json(['code'=>402,'msg'=>'失败']);
	        }
        }
    	
    	return $this->fetch();
    }
    public function goods_type_update()
    {
    	$id = input('id');
    	if($_POST){
    		$data['logo']=$_POST['logo'];
    		$data['type_name']=$_POST['type_name'];
            $result=Db::table('agent_type')->where('id',$id)->update($data);;
            if($result){
				return json(['code'=>200,'msg'=>'成功']);
	        }else{
	       		return json(['code'=>402,'msg'=>'失败']);
	        }
        }
        $list = Db::table('agent_type')->where('id',$id)->find();
        $this->assign('list',$list);
    	return $this->fetch();
    }
    public function goods_type_del()
    {
    	$id = input("id");
    	$res=Db::table('agent_goods')->where('trade_id',$id)->where('goods_state','<> 3')->find();
    	if($res){
			return json(['code'=>408,'msg'=>'该类有商品，不能删除']);
        }else{
       		$result = Db::table('agent_type')->where('id',$id)->delete();
       		if($result){
			return json(['code'=>200,'msg'=>'成功']);
	        }else{
	       		return json(['code'=>402,'msg'=>'失败']);
	        }
        }
    	
        
    	
    }
	//核销订单
	public function confirm(){
		$where['agent_id']=session('agent_id');
		$where['id']=input("id");
		$res=db('agent_syorder')->where($where)->update(["status"=>5]);
		if($res){
			return json(['code'=>200,'msg'=>'操作成功']);
		}else{
			return json(['code'=>400,'msg'=>'操作失败']);
		}
	}
    //订单管理
	public function goods_order()
    {
    	$where['o.agent_id']= session('agent_id');
		$where['o.type']=4;	
		if(input('sn')){
			$where['o.sn']=input('sn');
			$this->assign('sn',input('sn'));
		}else{
			$this->assign('sn',0);
		}
		if(input('status')){
			$where['o.status']=input("status");
			$this->assign('status',input('status'));
		}else{
			$this->assign('status',0);
		}
    	$list = Db::name('agent_syorder')->alias('o')->join("info_user u","o.user_id=u.id","left")
    	->join('agent_goods g','o.goods_id=g.id',"left")->where($where)->field("o.*,u.mobile,g.goods_name")->order('o.add_time desc')->paginate(15);
        $this->assign('list',$list);
    	return $this->fetch();
    }
    //评价管理
	public function evaluate()
    {
    	$list = Db::name('goods_comment')->where('agent_id',session('agent_id'))->order('add_time desc')->paginate(15);
        $this->assign('list',$list);
    	return $this->fetch();
    }
    
    //广告管理
	public function advertisement()
    {
    	$list=db("agent_advertisement")->where("agent_id",session('agent_id'))->select();
        $this->assign('list',$list);
		$this->assign('empty','<span class="empty">没有数据</span>');
    	return $this->fetch();
    }
    //是否上架
	public function shangjia()
    {
    	$data['goods_state'] = input("goods_state");
    	$res = Db::name('agent_goods')->where('id',input("id"))->update($data);
        if($res){
			return json(['code'=>200,'msg'=>'修改成功']);
		}else{
			return json(['code'=>402,'msg'=>'修改失败']);
		}
    }
    
	//是否推荐
	public function tuijian()
    {
    	$data['is_recommend'] = input("is_recommend");
    	$res = Db::name('agent_goods')->where('id',input("id"))->update($data);
        if($res){
			return json(['code'=>200,'msg'=>'修改成功']);
		}else{
			return json(['code'=>402,'msg'=>'修改失败']);
		}
    }
    
	
}
